package net.interfax;

import java.util.HashMap;
import java.util.Map;

import net.interfax.impl.XmlDeserializable;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @see http://www.interfax.net/en/dev/webservice/reference/faxstatus
 * @author DL
 */
public class FaxItem implements XmlDeserializable {
	public FaxItem() {}
	
	public int transactionID;
	
	/** The time when the transaction was originally submitted by the client. */
	public String submitTime;
	
	/** Time to which transmission was scheduled the transmission, or '0000-00-00' if not postponed. */
	public String postponeTime;
	
	/** End time of last of all transmission attempts. */
	public String completionTime;
	
	/**
	 * The fax number in a canonical format.
	 * @see http://www.interfax.net/en/dev/webservice/reference/special-formats
	 */
	public String destinationFax;
	
	/** Receiving party CSID (up to 20 characters). */
	public String remoteCSID;
	
	/** Number of successfully sent pages. */
	public int pagesSent;
	
	/**
	 * Fax status. 0= OK; greater than 0 = Error.
	 * @see http://www.interfax.net/en/help/error_codes
	 */
	public int status;
	
	/** Transmission time in seconds. */
	public int duration;
	
	/** Original value as submitted. */
	public String subject;
	
	@Override
	public String toString() {
		return "FaxItem [transactionID=" + transactionID + ", submitTime="
				+ submitTime + ", postponeTime=" + postponeTime
				+ ", completionTime=" + completionTime + ", destinationFax="
				+ destinationFax + ", remoteCSID=" + remoteCSID
				+ ", pagesSent=" + pagesSent + ", status=" + status
				+ ", duration=" + duration + ", subject=" + subject + "]";
	}

	@Override
	public void fromXml(Node node) {
		final NodeList childNodes = node.getChildNodes();
		
		final int numChildNodes = childNodes.getLength();
		final Map<String, String> values = new HashMap<String, String>(numChildNodes);
		for (int i = 0; i < numChildNodes; ++i) {
			final Node childNode = childNodes.item(i);
			values.put(childNode.getNodeName(), childNode.getTextContent());
		}
		
		transactionID = Integer.parseInt(values.get("TransactionID"));
		submitTime = values.get("SubmitTime");
		postponeTime = values.get("PostponeTime");
		completionTime = values.get("CompletionTime");
		destinationFax = values.get("DestinationFax");
		remoteCSID = values.get("RemoteCSID");
		pagesSent = Integer.parseInt(values.get("PagesSent"));
		status = Integer.parseInt(values.get("Status"));
		duration = Integer.parseInt(values.get("Duration"));
		subject = values.get("Subject");
	}
}
